#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2022/9/7 20:41
# @Author : cjw
from typing import Optional

from api_wework.api.wework_api import WeWorkApi


class DepartmentApi(WeWorkApi):
    """部门管理"""

    def create(self, name: str, parent_id: int, **kwargs):
        """
        创建部门
        :param name: 部门名称
        :param parent_id: 父部门id
        :param kwargs: 其它请求体参数
        :return:
        """
        # 创建部门的必须参数
        json_data = {'name': name, 'parentid': parent_id}
        # 创建部门的可选参数
        if kwargs:
            json_data.update(kwargs)

        # 构建请求数据
        data = {
            'method': 'post',
            'url': '/department/create',
            'params': {'access_token': self.token},
            'json': json_data
        }
        return self.request(data)

    def update(self, _id: int, **kwargs):
        """
        更新部门
        :param _id: 部门id
        :param kwargs:
        :return:
        """
        json_data = {'id': _id}
        if kwargs:
            json_data.update(kwargs)

        data = {
            'method': 'post',
            'url': '/department/update',
            'params': {'access_token': self.token},
            'json': json_data
        }
        return self.request(data)

    def delete(self, _id: int):
        """
        删除部门
        :param _id: 部门id
        :return:
        """
        res_data = {'id': _id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': '/department/delete',
            'params': res_data
        }
        return self.request(data)

    def get(self, _id: Optional[int] = None):
        """
        获取部门列表
        :param _id: 部门id，不填获取所有门数据
        :return:
        """
        res_data = {'id': _id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': '/department/list',
            'params': res_data
        }
        return self.request(data)

    def simple_list(self, _id: Optional[int] = None):
        """
        获取子部门ID列表
        :param _id: 部门id，不填获取所有门数据
        :return:
        """
        res_data = {'id': _id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': '/department/simplelist',
            'params': res_data
        }
        return self.request(data)

    def get_detail(self, _id: int):
        """
        获取单个部门详情
        :param _id: 部门id
        :return:
        """
        res_data = {'id': _id, 'access_token': self.token}

        data = {
            'method': 'get',
            'url': '/department/get',
            'params': res_data
        }
        return self.request(data)
